package com.gc.ykqx.service.presenter;


import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.gc.ykqx.service.entity.WeaResponse;
import com.gc.ykqx.service.manager.DataManager;
import com.gc.ykqx.service.view.QyWeaView;
import com.gc.ykqx.service.view.View;
import com.gc.ykqx.service.util.Constant;
import com.gc.ykqx.service.util.DateUtil;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by wsd on 2017/9/30.
 */

public class QyWeaPresenter implements Presenter {
    private static final String TAG = "QyWeaPresenter";
    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;
    private Context mContext;
    private QyWeaView mQyWeaView;
    private WeaResponse mWeaResponse;

    public QyWeaPresenter(Context context) {
        this.mContext = context;
    }

    @Override
    public void onCreate() {
        manager = new DataManager(mContext);
        mCompositeSubscription = new CompositeSubscription();

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }

    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(View view) {
        mQyWeaView = (QyWeaView) view;

    }

    @Override
    public void attachIncomingIntent(Intent intent) {

    }

    public void getWeather(String selectId, String staIds, String time, String siteType, String beginTime, String endTime, String diyType) {
        Log.e(TAG, "getWeather: " + selectId + " " + time + " " + siteType + " ");
        switch (selectId) {
            case "n00":
            //    getWeaMin(staIds, "20171225112000", Constant.Element.TEM);
                getWeaMin(staIds, time, Constant.Element.TEM);
                break;
            case "n01":
                getWeaHor(staIds, time, Constant.Element.TEM_INT);
                break;
            case "n02"://check time is today
                if (DateUtil.isToady(time)) {
                    getWeaEvsByStaIds(staIds, Constant.Element.TEM_MAX_20);
                } else {
                    getWeaDay(staIds, time, Constant.Element.TEM_MAX);
                }
                break;
            case "n03":
                if (DateUtil.isToady(time)) {
                    getWeaEvsByStaIds(staIds, Constant.Element.TEM_MIN_20);
                } else {
                    getWeaDay(staIds, time, Constant.Element.TEM_MIN);
                }
                break;
            case "n04":
                if (DateUtil.isToady(time)) {
                    getWeaEvsByStaIds(staIds, Constant.Element.TEM_AVG_20);
                } else {
                    getWeaDay(staIds, time, Constant.Element.TEM_AVG);
                }
                break;
            case "n10":
                getWeaEvsByTimeAndStaIds(staIds, time, Constant.Element.PRE_QY_10M);
                break;
            case "n11":
                getWeaEvsByTimeAndStaIds(staIds, time, Constant.Element.PRE_QY_30M);
                break;
            case "n12":
                getWeaFromHour(staIds, Constant.Element.PRE_QY_ZD);
                break;
            case "n13":
            case "n30":
                getWeaEvsByTimeAndStaIds(staIds, time, Constant.Element.PRE_QY_1H);
                break;
            case "n14":
            case "n31":
                getWeaEvsByTimeAndStaIds(staIds, time, Constant.Element.PRE_QY_3H);
                break;
            case "n32":
                getWeaEvsByTimeAndStaIds(staIds, time, Constant.Element.PRE_QY_6H);
                break;
            case "n15":
            case "n33":
                getWeaEvsByTimeAndStaIds(staIds, time, Constant.Element.PRE_QY_12H);
                break;
            case "n16":
            case "n34":
                getWeaEvsByStaIds(staIds, Constant.Element.PRE_QY_20);
                break;
            case "n17":
            case "n35":
                getWeaDay(staIds, time, Constant.Element.PRE_QY_2008);
                break;
            case "n18":
            case "n36":
                getWeaEvsByStaIds(staIds, Constant.Element.PRE_QY_08);
                break;
            case "n19":
            case "n37":
                getWeaDay(staIds, time, Constant.Element.PRE_QY_0820);
                break;
            case "n110":
            case "n38":
                if (DateUtil.isToady(time)) {
                    getWeaEvsByStaIds(staIds, Constant.Element.PRE_QY_20);
                } else {
                    getWeaDay(staIds, time, Constant.Element.PRE_QY_DAY);
                }
                break;
            case "n111":
            case "n39":
                if (diyType.equals(Constant.Diy.DAY)) {
                    getWeaDayCount(staIds, Constant.Element.PRE_QY_DAY, beginTime, endTime);
                } else if (diyType.equals(Constant.Diy.Hor)) {
                    getWeaHorCount(staIds, Constant.Element.PRE_QY_DAY, beginTime, endTime);
                } else if (diyType.equals(Constant.Diy.MIN)) {
                    getWeaMinCount(staIds, Constant.Element.PRE_QY_DAY, beginTime, endTime);
                }
                break;
            case "n40":
                getWeaMin(staIds, time, Constant.Element.WIND);
                break;
            case "n41":
                getWeaHor(staIds, time, Constant.Element.WIND_INT);
                break;
            case "n42":
                if (DateUtil.isToady(time)) {
                    getWeaEvsByStaIds(staIds, Constant.Element.WIND_MAX_20);
                } else {
                    getWeaDay(staIds, time, Constant.Element.WIND_MAX);
                }
                break;
        }
    }

    private void getWeaMin(String staIds, String time, String element) {
        mCompositeSubscription.add(manager.getWeaMin(staIds, time, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    // TODO: 2017/10/9 no date will forever load
    private void getWeaHor(String staIds, String time, String element) {
        mCompositeSubscription.add(manager.getWeaHor(staIds, time, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                        Log.e(TAG, "getWeaHor : " + weaResponse.getReturnMessage());
                    }
                }));

    }

    private void getWeaDay(String staIds, String time, String element) {
        mCompositeSubscription.add(manager.getWeaDay(staIds, time, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    /*
    分钟统计 10min 30min 不包括20点以来 和 整点以来
     */
    private void getWeaEvsByTimeAndStaIds(String staIds, String time, String element) {
        mCompositeSubscription.add(manager.getWeaEvsByTimeAndStaIds(staIds, time, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    /*
    08时以来 20时以来
     */
    private void getWeaEvsByStaIds(String staIds, String element) {
        mCompositeSubscription.add(manager.getWeaEvsByStaIds(staIds, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    /*
    整点以来
     */
    private void getWeaFromHour(String staIds, String element) {
        mCompositeSubscription.add(manager.getWeaFromHour(staIds, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    private void getWeaDayCount(String staIds, String element, String beginTime, String endTime) {
        mCompositeSubscription.add(manager.getWeaDayCount(staIds, element, beginTime, endTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    private void getWeaMinCount(String staIds, String element, String beginTime, String endTime) {
        mCompositeSubscription.add(manager.getWeaMinCount(staIds, element, beginTime, endTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

    private void getWeaHorCount(String staIds, String element, String beginTime, String endTime) {
        mCompositeSubscription.add(manager.getWeaHorCount(staIds, element, beginTime, endTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mQyWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }
}
